package com.cjx.stars.config;

import com.cjx.stars.interceptor.LoginInterceptor;
import com.cjx.stars.interceptor.RefreshTokenInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;

import static com.cjx.stars.constant.SystemConstants.CROSS_ORIGIN_ALLOWED_TIME;

/**
 * @author cjx
 * @description
 * @createDate 2024/2/29 20:00
 **/
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:5173", "http://127.0.0.1:5173")
                .allowedMethods("*")
                .allowCredentials(true)
                .maxAge(CROSS_ORIGIN_ALLOWED_TIME);

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 登录拦截器 TODO 修改登录拦截器不拦截的路径
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(
                        "/shop/**",
                        "/voucher/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/blog/hot",
                        "/user/code",
                        "/user/login",
                        "/user/message",
                        "/user/register",
                        "/user/current",
                        "/message"
                ).order(1);
        // token刷新的拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);
    }


}